﻿using System;
using System.Collections.Generic;

using PostSharp.Aspects;

namespace BizElements.BusinessLayer.Aspects
{
    /// <summary>
    /// Contains flags and data which specify how AOP execution flows.
    /// </summary>
    [Serializable]
    public sealed class AopExecutionToken
    {
        /// <summary>
        /// Initializes a new instance of <b>AopExecutionToken</b> class.
        /// </summary>
        public AopExecutionToken()
        {
            this.Items = new Dictionary<string, object>();
        }

        /// <summary>
        /// Gets a key/value collection that can be used to organize and share data during method execution.
        /// </summary>
        public IDictionary<string, object> Items { get; private set; }

        /// <summary>
        /// Ensures that <b>AopExecutionToken</b> is available in the <b>MethodExecutionArgs.MethodExecutionTag</b> property for the current method execution context.
        /// </summary>
        /// <param name="eventArgs">Event arguments specifying which method is being executed, which are its arguments, and how should the execution continue.</param>
        /// <returns>Existing or new instance of <b>AopExecutionToken</b> class.</returns>
        public static AopExecutionToken EnsureMethodExecutionTag(MethodExecutionArgs eventArgs)
        {
            AopExecutionToken aopControl = eventArgs.MethodExecutionTag as AopExecutionToken;
            if (aopControl == null)
            {
                aopControl = new AopExecutionToken();
                eventArgs.MethodExecutionTag = aopControl;
            }

            return aopControl;
        }
    }
}
